package com.jinbiao.quasar.sqltoy;

import cn.hutool.json.JSONUtil;
import com.jinbiao.quasar.util.RedisUtil;
import org.noear.redisx.RedisClient;
import org.sagacity.sqltoy.translate.cache.TranslateCacheManager;
import org.sagacity.sqltoy.translate.model.TranslateConfigModel;

import java.util.HashMap;

public class CustomTranslateCacheManager extends TranslateCacheManager {
    static final String prefix="sqltoy.translate:";

    public CustomTranslateCacheManager() {
        System.out.println(123);
        RedisClient client = RedisUtil.getClient();
        System.out.println(321);
    }

    private String buildKey(String cacheName, String cacheType){
        return prefix+":"+cacheName+":"+cacheType;
    }

    @Override
    public boolean hasCache(String s) {
        return RedisUtil.getClient().getBucket().exists(prefix+s);
    }

    @Override
    public HashMap<String, Object[]> getCache(String cacheName, String cacheType) {
        String result = RedisUtil.getClient().getBucket().get(buildKey(cacheName,cacheType));
        return JSONUtil.toBean(result,HashMap.class);

    }

    @Override
    public void put(TranslateConfigModel cacheModel, String cacheName, String cacheType, HashMap<String, Object[]> cacheValue) {
        RedisUtil.getClient().getBucket().store(buildKey(cacheName,cacheType),JSONUtil.toJsonStr(cacheValue),cacheModel.getKeepAlive());
    }

    @Override
    public void clear(String cacheName, String cacheType) {
        RedisUtil.getClient().getBucket().remove(buildKey(cacheName,cacheType));
    }

    @Override
    public boolean init() {
        return true;
    }

    @Override
    public void destroy() {

    }
}
